﻿using System;

namespace GLDotNet
{
	/// <summary>
	/// Contains static helper methods for OpenGL.
	/// </summary>
	public static class GLUtility
	{
        /// <summary>
        /// Calls to directly to glGetError without going through context object.
        /// </summary>
        /// <returns></returns>
        public static ErrorCode GetError()
        {
            return GLNative.GetError();
        }

        /// <summary>
        /// Decomposes a RGBA pixel into its individual components.
        /// </summary>
        /// <param name="pixel"></param>
        /// <param name="r"></param>
        /// <param name="g"></param>
        /// <param name="b"></param>
        /// <param name="a"></param>
        public static void DecomposePixelRGBA(uint pixel, out byte r, out byte g, out byte b, out byte a)
        {
            r = (byte)((pixel & 0xFF000000) >> 24);
            g = (byte)((pixel & 0x00FF0000) >> 16);
            b = (byte)((pixel & 0x0000FF00) >> 8);
            a = (byte)((pixel & 0x000000FF));
        }
     
        /// <summary>
        /// Builds a representation of an RGBA pixel.
        /// </summary>
        /// <param name="r">The red component.</param>
        /// <param name="g">The green component.</param>
        /// <param name="b">The blue component.</param>
        /// <param name="a">The alpha component.</param>
        /// <returns></returns>
        public static uint MakePixelRGBA(byte r, byte g, byte b, byte a)
        {
            return ((uint)r << 24) + ((uint)g << 16) + ((uint)b << 8) + (uint)a;
        }
               
		/// <summary>
		/// Replacement for gluPerspective.
		/// </summary>
		/// <param name="context"></param>
		/// <param name="fovY"></param>
		/// <param name="aspect"></param>
		/// <param name="zNear"></param>
		/// <param name="zFar"></param>
		public static void Perspective(GLContext context, double fovY, double aspect, double zNear, double zFar)
		{
			if (context == null)
				throw new ArgumentNullException("context");
						
			double yMax = zNear * Math.Tan(fovY * Math.PI / 360.0);
						
			context.Frustum(-yMax * aspect, yMax * aspect, -yMax, yMax, zNear, zFar);
		}
	}
}
